package com.jml.nio;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

//就是read和write的参数是一个ByteBuffer数组
public class 分散读取聚集写入 {

	public void fun3() throws Exception {//分散读取到缓冲区数组中
		RandomAccessFile raf1 = new RandomAccessFile("1.txt","rw");
		FileChannel channel1 = raf1.getChannel();

		ByteBuffer buffer1 = ByteBuffer.allocate(100);
		ByteBuffer buffer2 = ByteBuffer.allocate(1000);
		ByteBuffer[] buf = {buffer1,buffer2};
		channel1.read(buf);

		for (ByteBuffer buffer: buf){
			buffer.flip();
		}

		System.out.println(new String(buf[0].array(),0,buf[0].limit()));
		System.out.println(new String(buf[1].array(),0,buf[1].limit()));

		//聚集写入到缓冲区文件中中
		RandomAccessFile raf2 = new RandomAccessFile("1.txt","rw");
		FileChannel channel2 = raf1.getChannel();
		channel2.write(buf);
	}

}
